<?php
namespace Admin\Model;
use Common\Model\CommonModel;
class AdModel extends  CommonModel{
	
	//自动验证
	protected $_validate = array(
			//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
			array('title', 'require', '名称不能为空！', 1, 'regex', 3),
			
			array('content', 'require', '内容不能为空！', 0, 'regex', 3),
			
		
		
	);
	
	/**
	 * 后台有更新/编辑则删除缓存
	 * @param type $data
	 */
	public function _before_write() {
		parent::_before_write();
		$this->data_cache();
	}
	public function _after_insert() {
		parent::_after_insert();
		$this->data_cache();
	}
	//删除操作时删除缓存
	public function _after_delete() {
		parent::_after_delete();
		$this->data_cache();
	}
	public function data_cache(){
			
	
		$cate=D("AdCat")->select();
	
		foreach ($cate as $k=>$v){
			$result[$v[id]][id]=$v[id];
			$result[$v[id]][type]=$v[type];
			if($v[type]=="imgs"){
			$data=$this->where(array("status"=>1,"catid"=>$v[id]))->order(array("listorder" => "DESC","id"=>"DESC"))->select();
			
			$result[$v[id]]['list']=$data;
			
			}
			if($v[type]=="img"){
				
				$data=$this->where(array("status"=>1,"catid"=>$v[id]))->find();
				
				$result[$v[id]][thumb]=$data[thumb];
				$result[$v[id]][url]=$data[url];
				$result[$v[id]][content]=$data[content];
				
				
			}
			if($v[type]=="code"){
				
				$data=$this->where(array("status"=>1,"catid"=>$v[id]))->find();
				
				$result[$v[id]][content]=$data[content];
				
			}
			if($v[type]=="html"){
			
				$data=$this->where(array("status"=>1,"catid"=>$v[id]))->find();
			
				$result[$v[id]][content]=$data[content];
			
			}
			
			
				
			F("Ad",$result);
				
			
				
		}
	
	
			
	
			
			
	}
	
}